import 'package:app/common_widget/colors.dart';
import 'package:app/login_module/register_module/register_page.dart';
import 'package:app/net/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../common_widget/custom_indicator.dart';
import '../common_widget/hexcolor.dart';
import '../common_widget/my_webview.dart';
import '../home_module/home_page.dart';
import '../net/address.dart';
import '../net/persisten_storage.dart';
import 'code_login_page.dart';
import 'forget_password_page.dart';
import 'login_model.dart';
class LoginPage extends StatefulWidget {

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> with TickerProviderStateMixin {

  late TabController _controller;

  var usernameTF = TextEditingController();
  var passwordTF = TextEditingController();

  bool displayPsw = true;



  requestDataWithLogin(var login,var password)async{

    var params = {
      'login':login,
      'password':password,
    };

    var json = await DioManager().kkRequest(Address.login,bodyParams: params,isShowLoad: true);

    Usesr model = Usesr.fromJson(json);

    if (model.errmsg == 'ok') {
      // await prefs.setString('acctoken', _model.data.accessToken);
      PersistentStorage().setStorage('acctoken', model.data!.accessToken);
      PersistentStorage().setStorage('uid', model.data!.uid);
      PersistentStorage().setStorage('partner_id', model.data!.partnerId);
      PersistentStorage().setStorage('image128', model.data!.image128);
      PersistentStorage().setStorage('job_company_id', model.data!.jobCompanyId.toString());
      PersistentStorage().setStorage('job_user_id', model.data!.jobUserId.toString());
      PersistentStorage().setStorage('name', model.data!.name);
      PersistentStorage().setStorage('companyIdName', model.data!.companyIdName);
      PersistentStorage().setStorage('companyId', model.data!.companyId.toString());
      PersistentStorage().setStorage('company_ids_name', model.data!.companyIdsName);

      Get.offAll(HomePage());
      
      // Get.to(HomePage());

    } else {
      BotToast.showText(text: json['message']);
    }

  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = TabController(length: 2, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: false, // 就是这里

        // backgroundColor: Colors.white,
      // appBar: AppBar(
      //   actions: [
      //     IconButton(
      //         icon: const Icon(Icons.settings),
      //         onPressed: () {
      //           // Get.to(SetUrlPage());
      //         })
      //   ],
      //   backgroundColor: Colors.white,
      //   elevation: 0,
      // ),
      body:Stack(
        children: [
          Image.asset('images/login_bg.png',width: Get.width,height: Get.height,fit: BoxFit.cover,),
          Container(
            color: Colors.black54,
          ),
          Container(
            margin: const EdgeInsets.only(top: 100),
            child:Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  // color: Colors.red,
                    padding: const EdgeInsets.only(top: 25, left: 15, right: 15),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                ///gengx
                                Image.asset(
                                  'images/log_icon.png',
                                  width: 50,
                                  height: 50,
                                  // package: 'flutter_public_widget',
                                ),
                                Container(
                                  margin: const EdgeInsets.only(left: 8),
                                  child: Text(
                                    '登录DTCloud-APP'.tr,
                                    style: const TextStyle(
                                        fontSize: 22,
                                        color: Colors.white,
                                        fontWeight: FontWeight.w600),
                                  ),
                                )
                              ],
                            )
                            // Container(
                            //   margin: EdgeInsets.only(top: 5),
                            //   child: Text(
                            //     '海量教学课程,视频',
                            //     style: TextStyle(
                            //         fontSize: 12,
                            //         color: HexColor('#999999'),
                            //         fontWeight: FontWeight.w400),
                            //   ),
                            // )
                          ],
                        ),
                        // Image.asset(A.assets_loginright)
                      ],
                    )),
                Expanded(
                    child: Column(
                      children: [
                        Container(
                          margin: const EdgeInsets.only(top: 15),
                          width: Get.width,
                          decoration: BoxDecoration(
                              color: Colors.transparent,
                              borderRadius: BorderRadius.circular(0.0),
                             ),
                          child: TabBar(
                            isScrollable: true,
                            controller: _controller,

                            labelStyle: const TextStyle(
                              fontSize: 15,),

                            labelColor: Theme.of(context).primaryColor,

                            unselectedLabelStyle:
                            const TextStyle(fontSize: 14,),
                            unselectedLabelColor: Colors.white,
                            indicatorSize: TabBarIndicatorSize.label,

                            indicator: UnderlineTabIndicatorLi(
                                borderSide: BorderSide(
                                    color: Theme.of(context).primaryColor,
                                    width: 2,
                                    style: BorderStyle.solid)),
                            // indicatorColor: HexColor('#567BFD'),

                            tabs: [
                              Tab(
                                text: '账号登录'.tr,
                              ),
                              Tab(
                                text: '验证码登录'.tr,
                              ),
                            ],
                          ),
                        ),
                        Expanded(
                          child: TabBarView(
                            controller: _controller,
                            children: [
                              ListView(
                                children: [
                                  Center(
                                    child: Container(
                                      decoration: BoxDecoration(
                                        color: Colors.transparent,
                                        //设置四周圆角 角度
                                        borderRadius: const BorderRadius.all(
                                            Radius.circular(25.0)),
                                        //设置四周边框
                                        border: Border.all(
                                            width: 0.5, color: Colors.white),
                                      ),
                                      padding: const EdgeInsets.only(left: 15),
                                      margin: const EdgeInsets.only(
                                          top: 20, left: 15, right: 15),
                                      // width: Get.width-100,
                                      child: TextField(
                                        controller: usernameTF,
                                        autofocus: true,
                                        style: const TextStyle(fontSize: 16,color: Colors.white),
                                        inputFormatters: <TextInputFormatter>[
                                          LengthLimitingTextInputFormatter(13) //限制长度
                                        ],
                                        decoration: InputDecoration(
                                          // isCollapsed: true,
                                          // contentPadding: EdgeInsets.only(top: 15),
                                          hintText: '请输入账号/手机号'.tr,
                                          hintStyle: TextStyle(
                                              fontSize: 16, color: Colors.white.withAlpha(50)),
                                          border: InputBorder.none,
                                          // prefixIcon: Image.asset(A.assets_user,),
                                        ),
                                      ),
                                    ),
                                  ),
                                  Center(
                                    child: Container(
                                        decoration: BoxDecoration(
//背景
                                          color: Colors.transparent,
                                          //设置四周圆角 角度
                                          borderRadius: const BorderRadius.all(
                                              Radius.circular(25.0)),
                                          //设置四周边框
                                          border: Border.all(
                                              width: 0.5, color: Colors.white),
                                        ),
                                        padding: const EdgeInsets.only(left: 15),
                                        margin: const EdgeInsets.only(
                                            top: 10, right: 15, left: 15),
                                        child: Row(
                                          children: [
                                            SizedBox(
                                              width: Get.width - 100,
                                              child: TextField(
                                                obscureText: displayPsw,
                                                controller: passwordTF,
                                                style: const TextStyle(fontSize: 16,color: Colors.white),
                                                inputFormatters: <TextInputFormatter>[
                                                  // WhitelistingTextInputFormatter.digitsOnly,//只输入数字
                                                  LengthLimitingTextInputFormatter(
                                                      16) //限制长度
                                                ],
                                                decoration: InputDecoration(
                                                  // contentPadding: EdgeInsets.only(top: 15),
                                                  hintText: '请输入密码'.tr,
                                                  hintStyle: TextStyle(
                                                      fontSize: 16,
                                                      color: Colors.white.withAlpha(50)),
                                                  border: InputBorder.none,
                                                  // prefixIcon: Image.asset(A.assets_pwd),
                                                ),
                                              ),
                                            ),
                                            IconButton(
                                                onPressed: () {
                                                  displayPsw = !displayPsw;

                                                  setState(() {});
                                                },
                                                icon: Image.asset(
                                                  'images/showps.png',
                                                  width: 15,
                                                  height: 15,
                                                  // package: 'flutter_public_widget',
                                                ))
                                          ],
                                        )),
                                  ),


                                  GestureDetector(
                                    onTap: (){
                                      Get.to(const ForgetPassword());
                                    },
                                    child:Container(
                                      margin: const EdgeInsets.only(top: 10,left: 25),
                                      child: const Text('忘记密码?',style: TextStyle(color: Colors.white,
                                          fontSize: 12
                                      ),),
                                      // color: Colors.red,
                                    ),
                                  ),

                                  // GestureDetector(
                                  //   child: Container(
                                  //     padding: EdgeInsets.only(left: 25,top: 15,right: 25),
                                  //     // color: Colors.red,
                                  //     child: Row(
                                  //       mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  //       children: [
                                  //         Text('注册账号'.tr,style: TextStyle(fontSize: 14,color: HexColor('#2A39E1')),),
                                  //         Text('帮助手册'.tr,style: TextStyle(fontSize: 14,color: HexColor('#2A39E1')),),
                                  //       ],
                                  //     ),
                                  //
                                  //   ),
                                  //   onTap: (){
                                  //     Get.to(RegisterPage());
                                  //   },
                                  // ),

                                  Container(
                                    padding:
                                    const EdgeInsets.only(left: 25, top: 15, right: 25),
                                    // color: Colors.red,
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        GestureDetector(
                                          child: Text(
                                            '注册账号'.tr,
                                            style: const TextStyle(
                                                fontSize: 14,
                                                color: Colors.white),
                                          ),
                                          onTap: () {
                                            Get.to(const RegisterPage());
                                          },
                                        ),
                                        // GestureDetector(
                                        //
                                        //   child:  Text('手势登录'.tr,style: TextStyle(fontSize: 14,color: HexColor('#2A39E1')),),
                                        //   onTap: (){
                                        //     // Get.to(gesture_page());
                                        //     Get.to(GesturePasswordWidgetDemo());
                                        //   },
                                        // ),
                                        //  GestureDetector(
                                        //
                                        //    child:  Text('人脸登录'.tr,style: TextStyle(fontSize: 14,color: HexColor('#2A39E1')),),
                                        //    onTap: (){
                                        //      // Get.to(gesture_page());
                                        //      Get.to(FaceLoginPage());
                                        //      // Get.to(GesturePasswordWidgetDemo());
                                        //    },
                                        //  ),
                                        GestureDetector(
                                          child: Text(
                                            '帮助手册'.tr,
                                            style: const TextStyle(
                                                fontSize: 14,
                                                color: Colors.white),
                                          ),
                                          onTap: () {
                                            // Get.to(MyWebView(title: '使用说明',url: 'https://docs.qq.com/doc/DZmFZYVVKTldOQ3dp',));
                                            Get.to(MyWebView('使用说明',
                                                'https://docs.qq.com/doc/DZmFZYVVKTldOQ3dp'));
                                          },
                                        )
                                      ],
                                    ),
                                  ),

                                  Container(
                                    margin:
                                    const EdgeInsets.only(top: 62, right: 15, left: 15),
                                    width: Get.width,
                                    height: 45,
                                    child: MaterialButton(
                                      elevation: 0,
                                      shape: RoundedRectangleBorder(
                                        borderRadius: BorderRadius.circular(22),
                                      ),
                                      color: AppColor.themeColor,
                                      onPressed: () async {
                                        if(usernameTF.text.isEmpty||passwordTF.text.isEmpty){
                                          BotToast.showText(text: '请输入用户名密码');
                                          return;
                                        }

                                        requestDataWithLogin(usernameTF.text,passwordTF.text);
                                        FocusScope.of(context)
                                            .requestFocus(FocusNode());
                                      },
                                      child: Text(
                                        '登录'.tr,
                                        style: const TextStyle(color: Colors.white),
                                      ),
                                    ),
                                  ),

                                  Container(
                                    // color: Colors.red,
                                    margin: const EdgeInsets.only(top: 25),
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      children: [
                                        const Text(
                                          '阅读',
                                          style: TextStyle(
                                              fontSize: 12, color: Colors.white),
                                        ),
                                        GestureDetector(
                                          onTap: () {
                                            // Get.to();
                                            Get.to(MyWebView('', 'https://docs.qq.com/doc/DUUZSZW5la3FySVhP'));
                                          },
                                          child: Text(
                                            '《用户协议》《个人信息保护政策》',
                                            style: TextStyle(
                                                fontSize: 12,
                                                color: Theme.of(context).primaryColor),
                                          ),
                                        )
                                      ],
                                    ),
                                  )
                                ],
                              ),
                              const CodeLoginPage(),
                              // Container(),
                            ],
                          ),
                        ),
                      ],
                    ))
              ],
            ),
          ),
        ],
      )
    );
  }
}
